/**
 * 
 */
package edu.bbk.muc.lifetracker.logging;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.database.Cursor;
import edu.bbk.muc.lifetracker.DbAdapter;
import edu.bbk.muc.lifetracker.common.Acceleration;
import edu.bbk.muc.lifetracker.common.Event;
import edu.bbk.muc.lifetracker.common.SimpleLocation;

/**
 * Logs Location, Acceleration and Light values in KML file. Location values are
 * logged using Placemark, Acceleration and Light values are logged using
 * Description tags.
 * 
 * @author irenatyler and romankirillov
 * 
 */
public class LocationToolTipKMLLogger extends KMLLogger {

  String point; // content for Point tag, contains longitude, latitude and
                // altitude

  /**
   * Initialises this {@link LocationToolTipKMLLogger}'s and parent class
   * fields.
   * 
   * @param dbAdapter
   *          DB adapter which is used to read stored data.
   */
  public LocationToolTipKMLLogger(DbAdapter dbAdapter) {
    super(dbAdapter);
    type = "KML";
    fileExtension = ".kml";
  }

  @Override
  public String getFileExtension() {
    return fileExtension;
  }

  @Override
  public String getType() {
    return type;
  }

  /**
   * Gets string with longitude, latitude and altitude for Point tag.
   */
  private String getPoint(Event event) {
    SimpleLocation location = event.getLocation();
    return String.format("%1$s,%2$s,%3$s", location.getLon(), location.getLat(), location.getAlt());
  }

  /**
   * Gets Point tag with content.
   * 
   * @param event
   *          Event from which to get coordinates information.
   * @return Point tag with content
   */
  private String getPointTag(Event event) {
    return String.format("<Point><coordinates>%1$s</coordinates></Point>", getPoint(event));
  }

  /**
   * Gets event's timestamp in format needed for name tag.
   * 
   * @param event
   *          for which timestamp is being generated.
   * @return event's timestamp.
   */
  private String getTimeStampForName(Event event) {
    String timeStamp;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    timeStamp = format.format(new Date(event.getTimestamp()));
    return timeStamp;
  }

  /**
   * Gets description tag with content.
   * 
   * @param event
   *          Event from which to get description information.
   * @return description tag with content
   */
  protected String getDescTag(Event event) {
    return String.format("<description>%1$s</description>", getDesc(event));
  }

  /**
   * Gets name tag with content.
   * 
   * @param event
   *          Event number to include in the name
   * @return name tag with content
   */
  protected String getNameTag(Event event) {
    return String.format("<name>%1$s</name>", getName(event));
  }

  /**
   * Gets description information for description tag. This includes Timestamp
   * of {@link Event}, its Acceleration and Light sensor information.
   * 
   * @param event
   *          Event from which to get description information.
   * @return description information
   */
  String getDesc(Event event) {
    Acceleration acceleration = event.getAcceleration();
    StringBuilder result = new StringBuilder();
    if (null != acceleration) {
      String accelLateral = acceleration.getXAcceleration().toString();
      String accelLongitudinal = acceleration.getYAcceleration().toString();
      String accelVertical = acceleration.getZAcceleration().toString();
      result.append(String.format(
          "Acceleration: lateral - %1$s, longitudinal - %2$s, vertical - %3$s;", accelLateral,
          accelLongitudinal, accelVertical));
    }
    if (null != event.getAmbientLight()) {
      result.append(" Light: ").append(event.getAmbientLight());
    }

    return result.toString();
  }

  /**
   * Creates name content for Name tag as Event number.
   * 
   * @param event
   *          event number
   * @return name content as Event number.
   */
  String getName(Event event) {
    return String.format("Event at: %1$s", getTimeStampForName(event));
  }

  @Override
  String getXmlBody() {
    Cursor c = dbAdapter.fetchAllEvents();
    xmlBody = "";
    int eventNo = 0; // event number
    Event curEvent; // current event
    // for each logged event create Placemark tag
    c.moveToFirst();
    while (!c.isAfterLast()) {
      eventNo++;
      curEvent = Event.fromJSON(c.getString(1));
      // only create Placemark if there is location information for this event
      // as it doesn't make sense to show event in Google Earth without location
      if (curEvent.getLocation() != null) {
        xmlBody =
            xmlBody
                + String.format("<Placemark>%1$s%2$s%3$s</Placemark>", getNameTag(curEvent),
                    getDescTag(curEvent), getPointTag(curEvent));
      }
      c.moveToNext();
    }
    c.close();
    return xmlBody;
  }

}
